package com.molyfun.parents.activity.coursecenter.view;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.molyfun.parents.R;
import com.molyfun.parents.activity.coursecenter.TargetDetailActivity;
import com.molyfun.parents.rest.bean.ProductatalogListResponse;
import com.molyfun.parents.utils.TimeUtils;
import com.molyfun.parents.utils.UIUtils;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by william(liuchao.hit@gmail.com).
 * Date:10/18/16.
 */
public class ProductCatalogView extends LinearLayout implements View.OnClickListener {

    protected LayoutInflater layoutInflater;

    private Context context;

    @InjectView(R.id.catalog_title)
    TextView mCatalogTitle;
    @InjectView(R.id.catalog_content)
    TextView mCatalogContent;
    //    @InjectView(R.id.ic_srrow)
//    ImageView mIcSrrow;
    @InjectView(R.id.product_catalog_target)
    TextView mProductCatalogTarget;
    @InjectView(R.id.product_icon)
    ImageView mProductIcon;
    @InjectView(R.id.zhibo_state)
    ImageView mZhiboState;
    @InjectView(R.id.line)
    View mLine;

    private boolean mIsChange = false;

    private ProductatalogListResponse.ProductCatalogBrief mProductCatalogBrief;

    private String mSubjectId;//用于分享

    public ProductCatalogView(Context context, AttributeSet attrs, int defStyle) {
        this(context, attrs);
    }

    public ProductCatalogView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public ProductCatalogView(Context context) {
        super(context);
        init(context, null);
    }

    private void init(Context context, AttributeSet attrs) {
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
        layoutInflater.inflate(R.layout.widget_product_catalog, this);
        ButterKnife.inject(this);
//        setOnClickListener(this);
    }


//    public void initData(ProductatalogListResponse.ProductCatalogBrief productCatalogBrief) {
//        initData(productCatalogBrief, false, false);
//    }

    /**
     * init view
     */
    public void initData(ProductatalogListResponse.ProductCatalogBrief productCatalogBrief, boolean isLast, boolean isCharge, String subjectId) {
        if (productCatalogBrief == null) {
            return;
        }
        mCatalogTitle.setTextColor(ContextCompat.getColor(getContext(), R.color.color_title_text));
        mCatalogContent.setTextColor(ContextCompat.getColor(getContext(), R.color.color_title_text));
        mProductCatalogBrief = productCatalogBrief;
        mCatalogTitle.setText(mProductCatalogBrief.getName());
        mSubjectId = subjectId;
        if (TextUtils.isEmpty(productCatalogBrief.getTeachway()) || productCatalogBrief.getTeachway().equals("media")) {
            mZhiboState.setVisibility(View.GONE);
            mProductIcon.setImageResource(R.mipmap.icon_class_catalog_playback);
            mCatalogContent.setText("视频课随时可观看");
        } else {
            if (!TextUtils.isEmpty(mProductCatalogBrief.getLivestatus())) {
                if (mProductCatalogBrief.getLivestatus().equals("init")) {
                    mProductIcon.setImageResource(R.mipmap.ic_zhiboing_wait_start);
                    mZhiboState.setVisibility(View.GONE);
                    mCatalogTitle.setTextColor(ContextCompat.getColor(getContext(), R.color.color_split));
                    mCatalogContent.setTextColor(ContextCompat.getColor(getContext(), R.color.color_split));
//                    mCatalogContent.setText("上课时间 " + TimeUtils.getDateMinuteByTimeMillis(productCatalogBrief.getBegindatetime()) + "～" + TimeUtils.getDateMinuteByTimeMillis(productCatalogBrief.getEnddatetime()));
                    mCatalogContent.setText("上课时间 " + TimeUtils.getDateMinuteByTimeMillis(productCatalogBrief.getBegindatetime()) + "～" + TimeUtils.getDateMinuteByTimeMillis(productCatalogBrief.getEnddatetime()).split(" ")[1]);
                } else if (mProductCatalogBrief.getLivestatus().equals("prog")) {
                    mProductIcon.setImageResource(R.mipmap.icon_class_catalog_live);
                    mZhiboState.setVisibility(View.VISIBLE);
                    mZhiboState.setImageResource(R.mipmap.ic_zhiboing);
//                    mCatalogContent.setText("上课时间 " + TimeUtils.getDateMinuteByTimeMillis(productCatalogBrief.getBegindatetime()) + "～" + TimeUtils.getDateMinuteByTimeMillis(productCatalogBrief.getEnddatetime()));
                    mCatalogContent.setText("上课时间 " + TimeUtils.getDateMinuteByTimeMillis(productCatalogBrief.getBegindatetime()) + "～" + TimeUtils.getDateMinuteByTimeMillis(productCatalogBrief.getEnddatetime()).split(" ")[1]);
                } else if (mProductCatalogBrief.getLivestatus().equals("playbacking")) {
                    mProductIcon.setImageResource(R.mipmap.icon_class_playback_wait);
                    mZhiboState.setVisibility(View.GONE);
                    mZhiboState.setImageResource(R.mipmap.ic_callback_creating);
                    mCatalogTitle.setTextColor(ContextCompat.getColor(getContext(), R.color.color_split));
                    mCatalogContent.setTextColor(ContextCompat.getColor(getContext(), R.color.color_split));
                    mCatalogContent.setText("回放生成中");
                } else if (mProductCatalogBrief.getLivestatus().equals("playback")) {
                    mProductIcon.setImageResource(R.mipmap.icon_class_catalog_playback);
                    mCatalogContent.setText("直播回看");
                    mZhiboState.setVisibility(View.GONE);
                }
            } else {
                mZhiboState.setVisibility(View.GONE);
            }

        }

        mProductCatalogTarget.setText(mProductCatalogBrief.getTarget());
        setOnClickListener(this);


        if (isLast) {
            mLine.setVisibility(View.GONE);
        } else {
            mLine.setVisibility(View.VISIBLE);
        }
        mIsChange = isCharge;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                if (mIsChange) {
                    TargetDetailActivity.startActivity(getContext(), mProductCatalogBrief.getId(), mSubjectId);
                } else {
                    UIUtils.showToast(getContext(), "购买后可查看全部课程内容");
                }
                break;

        }
    }

}